﻿using Microsoft.Net.Http.Headers;
using System.Diagnostics.CodeAnalysis;

namespace ExceptionHandlingDemo.Api.Extensions;

public static class HttpRequestExtensions
{
    public static bool IsAjax([NotNull] this HttpRequest request)
    {
        return string.Equals(request.Query[HeaderNames.XRequestedWith], "XMLHttpRequest", StringComparison.Ordinal) ||
               string.Equals(request.Headers[HeaderNames.XRequestedWith], "XMLHttpRequest", StringComparison.Ordinal);
    }

    public static bool CanAccept([NotNull] this HttpRequest request, [NotNull] string contentType)
    {
        return request.Headers[HeaderNames.Accept].ToString().Contains(contentType, StringComparison.OrdinalIgnoreCase);
    }
}